﻿/************************************************************************

 This file is part of "10 Second Tim".

 "10 Second Tim" is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 "10 Second Tim" is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with "10 Second Tim".  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2013 Scott Bevin. all rights reserved

using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace LDE.Input
{
	public enum MouseInputs
	{
		LeftButton = 0,
		MiddleButton,
		RightButton,

		Max,
	}

#if WINDOWS || SILVERLIGHT

	public class MouseInputDevice : IInputDevice
	{
		private static int s_deviceTypeId = ( "Mouse" ).GetHashCode( );

		public static int DeviceTypeId { get { return s_deviceTypeId; } }

		private class MouseButton : InputButton
		{
			public MouseButton( MouseInputs input )
			{
				Button = input;
			}

			public MouseInputs Button = MouseInputs.LeftButton;
		}

		private List<MouseButton> m_buttons = new List<MouseButton>( (int)MouseInputs.Max );

		public int GetDeviceTypeId( )
		{
			return s_deviceTypeId;
		}

		public int GetDeviceId( )
		{
			return 0;
		}

		public MouseInputDevice( )
		{
			m_buttons.Add( new MouseButton( MouseInputs.LeftButton ) );
			m_buttons.Add( new MouseButton( MouseInputs.MiddleButton ) );
			m_buttons.Add( new MouseButton( MouseInputs.RightButton ) );
		}

		public InputButton IsPressed( InputTrigger trigger )
		{
			foreach ( MouseInputs m in trigger.MouseButtons )
			{
				foreach ( MouseButton button in m_buttons )
				{
					if ( button.Button == m )
					{
						if ( button.Down && !button.DownLF )
							return button;
					}
				}
			}

			return null;
		}

		public InputButton IsHeld( InputTrigger trigger )
		{
			foreach ( MouseInputs m in trigger.MouseButtons )
			{
				foreach ( MouseButton button in m_buttons )
				{
					if ( button.Button == m )
					{
						if ( button.Down && button.CurrStateTimer >= InputManager.Holdtime )
							return button;
					}
				}
			}

			return null;
		}

		public InputButton IsDown( InputTrigger trigger )
		{
			foreach ( MouseInputs m in trigger.MouseButtons )
			{
				foreach ( MouseButton button in m_buttons )
				{
					if ( button.Button == m )
					{
						if ( button.Down )
							return button;
					}
				}
			}

			return null;
		}

		public InputButton IsReleased( InputTrigger trigger )
		{
			foreach ( MouseInputs m in trigger.MouseButtons )
			{
				foreach ( MouseButton button in m_buttons )
				{
					if ( button.Button == m )
					{
						if ( !button.Down && button.DownLF )
							return button;
					}
				}
			}

			return null;
		}

		public InputButton IsTapped( InputTrigger trigger )
		{
			foreach ( MouseInputs m in trigger.MouseButtons )
			{
				foreach ( MouseButton button in m_buttons )
				{
					if ( button.Button == m )
					{
						if ( !button.Down && button.DownLF && button.PrevStateTimer < InputManager.TapTime )
							return button;
					}
				}
			}

			return null;
		}

		public void UpdateState( float dt )
		{
			MouseState state = Mouse.GetState( );

			for ( int i = 0; i < m_buttons.Count; ++i )
			{
				m_buttons[ i ].DownLF = m_buttons[ i ].Down;
			}

			m_buttons[ (int)MouseInputs.LeftButton ].Down = ( state.LeftButton == ButtonState.Pressed );
			m_buttons[ (int)MouseInputs.MiddleButton ].Down = ( state.MiddleButton == ButtonState.Pressed );
			m_buttons[ (int)MouseInputs.RightButton ].Down = ( state.RightButton == ButtonState.Pressed );

			for ( int i = 0; i < m_buttons.Count; ++i )
			{
				MouseButton button = m_buttons[ i ];

				if ( button.Down != button.DownLF )
				{
					button.PrevStateTimer = button.CurrStateTimer;
					button.CurrStateTimer = 0.0f;
				}

				button.CurrStateTimer += dt;
			}
		}

		public bool IsConnected( )
		{
			// Todo, working this one out
			return true;
		}

		public Microsoft.Xna.Framework.Vector2 GetStickVals( )
		{
			return Vector2.Zero;
		}

		public bool IsPointingDevice( )
		{
			return true;
		}

		public Point GetPointerPos( )
		{
			return new Point( Mouse.GetState( ).X, Mouse.GetState( ).Y );
		}
	}

#endif
}